.data
	x: .long 0,0,0,0,0,0,0,0,0,0				# x for function
	sum: .long 0						# summary of all measured values
	str: .string "%d\n"					# result for measured values in f(x)

.text
.globl main
main:
	movl $0, %ebx						# index starts with 0

.L1:
	cmpl $10, %ebx						# while index != 10
	je .L2							# i == 10 go to L2
	
	pushl $x						# push x into stack
	pushl $str						# push string into stack
	call scanf						# call scan
	addl $8, %esp						# fill stackpointer

	addl $1, %ebx						# iteration i + 1
	jmp .L1							# go to L1 again
	
.L2:
	cmpl $10, %edi						# while index != 10
	je .L3							# if j == 10 go to L3
	
	movl x(,%edi,4), %eax					# move x into %eax reg
	movl sum, %ebx						# move sum into %ebx reg
	pushl %eax						# push %eax into stack
	pushl %ebx						# push %ebx into stack
	call fx							# call function f(x) = x^2 + 2x - 2
	
	addl $1, %edi						# iteration j + 1
	jmp .L2							# go to L2 again

.L3:
	movl $10, %ebx						# move $10 (10 iterations for measured values) into %ebx
	cltd
	idivl %ebx						# %eax (sum) / %ebx ($10)
	
	pushl %ebx						# push %ebx into stack
	pushl %eax						# push %eax into stack
	pushl $str						# push output string into stack
	call printf						# call print
	addl $8, %esp						# fill stackpointer
	
	movl $1, %eax						# exit
	int $0x80						# software interrupt

.globl fx
fx:
	pushl %ebp						# push basepointer
	movl %esp, %ebp						# move stackpointer into basepointer
	
	movl 12(%ebp), %eax					# move x into %eax
	movl 8(%ebp), %ebx					# move sum into %ebx
	pushl %ebx

	movl x, %ebx						# move x into %ebx reg
	imull %eax						# x * x
	pushl %eax						# push %eax (x * x) into stack
	movl x, %eax						# move x into %eax reg
	movl $2, %ebx						# move 2 into %ebx reg
	imull %ebx						# 2 * x
	xchgl %eax, %ebx					# change values in regs
	popl %eax						# pop %eax (x * x)
	addl %ebx, %eax						# (x * x) + (2 * x)
	subl $2, %eax						# (x * x) + (2 * x) - 2

	popl %ebx						# pop sum
	addl %ebx, %eax						# measured value + sum of all measured values

	movl %eax, 8(%ebp)					# move %ebx into basepointer
        popl %ebp						# pop basepointer
	ret